package com.itheima.test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JFrameButtonClick extends JFrame implements ActionListener {
    //创建按钮
    JButton jButton1 = new JButton("点击我");
    JButton jButton2 = new JButton("来点击我");

    public JFrameButtonClick(){

        JFrame jFrame = new JFrame();
        jFrame.setTitle("测试点击按钮");
        jFrame.setSize(400,400);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(null);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        jButton1.setBounds(0,0,100,50);
        jButton1.addActionListener(this);
        //添加按钮
        jFrame.getContentPane().add(jButton1);

        jButton2.setBounds(100,0,100,50);
        jButton2.addActionListener(this);
        //添加按钮
        jFrame.getContentPane().add(jButton2);




        jFrame.setVisible(true);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
       if ( e.getSource()== jButton1){
           System.out.println("我被点击了jButton1");
       }else  if ( e.getSource()== jButton2){
           System.out.println("我被点击了jButton2");
       }
    }
}
